{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "oriental-horizontal",
   "metadata": {},
   "source": [
    "# French Company Establishment Identification Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "conceptual-liverpool",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "rubber-artist",
   "metadata": {},
   "source": [
    "The function `clean_fr_siret()` cleans a column containing French company establishment identification number (SIRET) strings, and standardizes them in a given format. The function `validate_fr_siret()` validates either a single SIRET strings, a column of SIRET strings or a DataFrame of SIRET strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "alleged-going",
   "metadata": {},
   "source": [
    "SIRET strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"73282932000074\"\n",
    "* `standard`: SIRET strings with proper whitespace in the proper places, like \"732 829 320 00074\"\n",
    "* `siren`: convert the SIRET number to a SIREN number, like \"732829320\".\n",
    "* `tva`: convert the SIRET number to a TVA number, like \"44732829320\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_fr_siret()` and `validate_fr_siret()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "featured-fitness",
   "metadata": {},
   "source": [
    "### An example dataset containing SIRET strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "convenient-catering",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"siret\": [\n",
    "            \"73282932000074\",\n",
    "            \"73282932000079\",\n",
    "            \"999 999 999\",\n",
    "            \"004085616\",\n",
    "            \"002 724 334\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "outstanding-export",
   "metadata": {},
   "source": [
    "## 1. Default `clean_fr_siret`\n",
    "\n",
    "By default, `clean_fr_siret` will clean siret strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "opened-cuisine",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_fr_siret\n",
    "clean_fr_siret(df, column = \"siret\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "resistant-delight",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "special-grenada",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "colonial-terrain",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "legitimate-motion",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siret(df, column = \"siret\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "composite-thompson",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tropical-gravity",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siret(df, column = \"siret\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cardiac-pottery",
   "metadata": {},
   "source": [
    "### `siren`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bibliographic-france",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siret(df, column = \"siret\", output_format=\"siren\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "forbidden-barrier",
   "metadata": {},
   "source": [
    "### `tva`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tested-copyright",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siret(df, column = \"siret\", output_format=\"tva\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "miniature-adobe",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned SIRET strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "incident-version",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siret(df, column=\"siret\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "handy-progressive",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "widespread-conducting",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "superb-retrieval",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siret(df, \"siret\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "forty-affect",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "artificial-administration",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_fr_siret(df, \"siret\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "approved-seating",
   "metadata": {},
   "source": [
    "## 4. `validate_fr_siret()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "charming-rover",
   "metadata": {},
   "source": [
    "`validate_fr_siret()` returns `True` when the input is a valid SIRET. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_fr_siret()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_fr_siret()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_fr_siret()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "conventional-bosnia",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_fr_siret\n",
    "print(validate_fr_siret(\"73282932000074\"))\n",
    "print(validate_fr_siret(\"73282932000079\"))\n",
    "print(validate_fr_siret(\"999 999 999\"))\n",
    "print(validate_fr_siret(\"51824753556\"))\n",
    "print(validate_fr_siret(\"004085616\"))\n",
    "print(validate_fr_siret(\"hello\"))\n",
    "print(validate_fr_siret(np.nan))\n",
    "print(validate_fr_siret(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bronze-personal",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "nervous-scout",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fr_siret(df[\"siret\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "overhead-preservation",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "matched-license",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_fr_siret(df, column=\"siret\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "pursuant-sterling",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "other-cornell",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "validate_fr_siret(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "intensive-right",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
